# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2019/1/25
# python-version    : Python 3.7.0
# description       : 文章操作相关接口

import os, redis, json
from flask import Flask, jsonify, request, session
from flask_session import Session
from com.articles.microapp.service.articles_service import ArticleService
from com.articles.microapp.service.remarks_service import RemarkService
from com.articles.microapp.utils.logger_utils import Logger
from com.articles.microapp.utils.constants import CommonConstants
from com.articles.microapp.config.config import CONFIG

from com.articles.microapp.controller.app_controller import WechatInterface

consts = CommonConstants()
config = CONFIG.config()

articles_service = ArticleService()
remarks_service = RemarkService()
logger = Logger()

# 设置SECRET_KEY
SECRET_KEY = os.urandom(36)

app = Flask(__name__)
# 关闭ascii编码，支持中文
app.config['JSON_AS_ASCII'] = False
# app.config['SECRET_KEY'] = SECRET_KEY
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = redis.Redis(host=config.get_value(consts.CACHE_SECTION, consts.CACHE_HOST),
                                          port=int(config.get_value(consts.CACHE_SECTION, consts.CACHE_PORT)))
Session(app)


@app.route('/login')
def login():
    session["userId"] = '28045612534'
    return 'success'


@app.route('/getArticles', methods=['GET'])
def get_articles():
    page_no = int(request.args.get('pageNo'))
    page_size = int(request.args.get('pageSize'))
    start = (page_no - 1) * page_size
    articles = articles_service.query_articles(page=[start, page_size])
    return jsonify(articles)


@app.route('/getBanners', methods=['GET'])
def get_banner():
    cnt = consts.DEFAULT_PAGE_SIZE
    if request.args.get('cnt'):
        cnt = int(request.args.get('cnt'))
    banner = articles_service.query_banner(page=[consts.DEFAULT_PAGE_NO, cnt])
    return jsonify(banner)


@app.route('/getArticle', methods=['GET'])
def get_article():
    article_id = request.args.get('articleId')
    article = articles_service.query_article(article_id)
    user_id = '28045612534'
    # print(session)
    article.isLocked = remarks_service.is_locked(article.articleId, user_id)
    return jsonify(article.__dict__)


@app.route('/getRemarks', methods=['GET'])
def get_remarks():
    article_id = request.args.get('articleId')
    page_no = int(request.args.get('pageNo'))
    page_size = int(request.args.get('pageSize'))
    start = (page_no - 1) * page_size
    user_id = '28045612534'
    remarks = remarks_service.get_remarks(article_id, user_id, page=[start, page_size])
    return jsonify(remarks)


@app.route('/addRemark', methods=['POST'])
def add_remark():
    data = request.data
    if data:
        args = json.loads(data)
        article_id = args['articleId']
        content = args['content']
        user_id = '28045612534'
        return jsonify(remarks_service.add_remark(article_id, user_id, content))
    else:
        return jsonify(False)


@app.route('/like', methods=['GET'])
def like():
    id = request.args.get('objId')
    user_id = "28045612534"
    return jsonify(remarks_service.add_like(id, user_id))


@app.route('/cancelLike', methods=['GET'])
def cancel_like():
    id = request.args.get('objId')
    user_id = "28045612534"
    return jsonify(remarks_service.cancel_like(id, user_id))


if __name__ == '__main__':
    wechat = WechatInterface(app)
    app.run(host='192.168.241.80', port='5000')

